package com.saber;

import com.rabbitmq.client.*;
import com.saber.util.RabbitmqUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by PC on 2017/10/18.
 */
public class ReceiveLogs1 {

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitmqUtil.buildConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EmitLog.EXCHANGE_NAME, "fanout");

        //产生一个随机的队列名称
        String queueName = channel.queueDeclare().getQueue();
        //对队列进行绑定
        channel.queueBind(queueName, EmitLog.EXCHANGE_NAME, "");

        System.out.println("ReceiveLogs1 Waiting for messages");

        com.rabbitmq.client.Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);

                String message = new String(body,"UTF-8");
                System.out.println("ReceiveLogs1 Recevied ' " + message +"'");
            }
        };
        //队列会自动删除
        channel.basicConsume(queueName,true,consumer);
    }
}
